<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">
          <img src="@/assets/common/login-logo.png" alt="" />
        </h3>
      </div>

      <el-form-item prop="mobile">
        <el-input
          prefix-icon="el-icon-user-solid"
          ref="username"
          v-model="loginForm.mobile"
          placeholder="手机号"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          prefix-icon="el-icon-lock"
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
          show-password
        />
      </el-form-item>

      <el-button
        :loading="loading"
        class="btn"
        style="width:100%;margin-bottom:30px;"
        @click.native.prevent="handleLogin"
        >登录</el-button
      >
      <div class="tip">还没有账号？立即注册</div>
    </el-form>
  </div>
</template>

<script>
import { validMobile } from '@/utils/validate'
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data () {
    const validateMobile = (rule, value, callback) => {
      validMobile(value) ? callback() : callback(new Error('手机号格式不正确'))
    }
    return {
      loginForm: {
        mobile: '13800000002',
        password: '123456'
      },
      loginRules: {
        mobile: [
          { required: true, trigger: 'blur', message: '手机号不能为空' },
          {
            validator: validateMobile,
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' },
          {
            min: 6,
            max: 16,
            message: '密码的长度在6-16位之间 ',
            trigger: 'blur'
          }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    ...mapActions(['user/login']),
    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin () {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          this.loading = true
          try {
            await this['user/login'](this.loginForm)
            // 接收未登录跳转的参数
            const _redirect = this.$route.query._redirect
            _redirect ? this.$router.push(_redirect) : this.$router.push('/')
            this.$message.success('登陆成功')
          } catch (error) {
            console.log('登陆出错：' + error.message)
          } finally {
            this.loading = false
          }
        }
      })
    }
  }
}
</script>
<style lang="scss">
.login-container {
  height: 100%;
  background-image: url('~@/assets/common/login.jpg');
  background-position: center;
  .login-form {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    input.el-input__inner {
      height: 50px;
      border: none;
    }
    .btn {
      background-color: #4080fe;
      border: none;
      border-radius: 5px;
      font-size: 20px;
      color: #fff;
      height: 50px;
    }
    .tip {
      text-align: center;
      color: #fff;
    }
    .el-form-item__error {
      color: #fff;
    }
  }
}
</style>
